package com.syh.service.impl;

import com.syh.mapper.CronMapper;
import com.syh.mapper.OrdersMapper;
import com.syh.mapper.ShopMapper;
import com.syh.pojo.Cron;
import com.syh.pojo.Orders;
import com.syh.pojo.Shop;
import com.syh.pojo.ShopExample;
import com.syh.service.ShopService;
import com.syh.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author syh
 * @date 2023/3/23-16:57
 */
@Service
@SuppressWarnings("all")
public class ShopServiceImpl implements ShopService {
    @Autowired
    ShopMapper shopMapper;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    ShopService shopService;
    @Autowired
    CronMapper cronMapper;
    @Override  //查询所有商品
    public List<Shop> selectAll() {
        ShopExample example = new ShopExample();
        List<Shop> shopList=shopMapper.selectByExample(example);
//        List<Shop> shopList=new LinkedList<>();
        return shopList;
    }

    @Override
    public void insertshop(Shop shop) {
        shopMapper.insert(shop);
    }

    @Override
    public void updateshop(Shop shop) {
        Random r=new Random();

        shop.setGoodsNumber(r.nextInt(15)+1);
        shopMapper.updateByPrimaryKey(shop);
    }

    @Override
    public void updateshopbyprices(Shop shop) {
        Random r=new Random();
        Double e=r.nextDouble()*20+1;
        shop.setGoodsPrice(e);
        shopMapper.updateByPrimaryKey(shop);
    }

    @Override
    public void deleteshop(Shop shop) {
        shopMapper.deleteByPrimaryKey(shop.getId());
    }

    @Override
    public void shopping(Shop s,String username) {
        int oid=1111;
//        for (Shop s:shopList){
            Shop shop=shopMapper.selectByPrimaryKey(s.getId());
            if(shop.getGoodsNumber()<s.getGoodsNumber()){
                int i=1/0;
            }
            else {
                shop.setGoodsNumber(shop.getGoodsNumber()-s.getGoodsNumber());
                shopMapper.updateByPrimaryKey(shop);
                Orders order=new Orders();
                order.setOrderId(oid);
                order.setGoodsId(s.getId());
                order.setGoodsPrice(shop.getGoodsPrice());
                order.setProductionTime(shop.getProductionTime());
                order.setShopTime(new Date());
                order.setRemarks("1111");
                order.setUserName(username);
                order.setGoodsName(shop.getGoodsName());
                order.setOrderPrice(shop.getGoodsPrice()*s.getGoodsNumber());
                order.setOrderNumber(s.getGoodsNumber());
                ordersMapper.insert(order);
//            }
        }
    }

    @Override
    public void bh() {
        List<Shop> shopList=shopService.selectByNumber();
        for (Shop s : shopList) {
            shopService.updateshop(s);
        }
        Cron cron=new Cron();
        cron.setCronId("2");
        Random r=new Random();
        int time=r.nextInt(100)+10;
        String cronTime="0/"+time+" * * * * ?";
        cron.setCron(cronTime);
        cronMapper.updateByPrimaryKey(cron);

    }

    @Override
    public List<Shop> selectByNumber() {
        ShopExample shopExample=new ShopExample();
        shopExample.createCriteria().andGoodsNumberEqualTo(0);
        List<Shop> shopList=shopMapper.selectByExample(shopExample);
        return shopList;
    }

    @Override
    public Shop selectByID(Integer id) {
        Shop shop=shopMapper.selectByPrimaryKey(id);
        return shop;
    }

}
